package Models;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author Thomas
 */
public class ItemCollection
{
	private ArrayList<Item> items;
    private static ItemCollection instance = new ItemCollection();

	private ItemCollection()
	{
		this.items =  new ArrayList<Item>();
        this.items.add(new Item("skrue", "til at holde ting med", "Skruer og Søm Inc", SupplierCollection.getInstance().getSupplier(101), 100d, Location.HardwareStore, 250, 10000));
		this.items.add(new Item("stige", "til at stå på", "Stiger og trapper Inc", SupplierCollection.getInstance().getSupplier(100), 250d, Location.HardwareStore, 10, 25));
		this.items.add(new Item("træ", "til at bygge med", "Træ og mursten Inc", SupplierCollection.getInstance().getSupplier(102), 80d, Location.LumberYard, 100, 250));
		this.items.add(new Item("mursten", "til at bygge med", "træ og mursten Inc", SupplierCollection.getInstance().getSupplier(101), 150d, Location.LumberYard, 1000, 2500));
		this.items.add(new Item("Ege skuffe", "til at have ting i", "Kitchen Inc", SupplierCollection.getInstance().getSupplier(100), 547, Location.HardwareStore, 254, 800));
		this.items.add(new Item("Bøge skuffe", "til at have ting i", "Kitchen Inc", SupplierCollection.getInstance().getSupplier(100), 258, Location.HardwareStore, 84, 700));
		this.items.add(new Item("Ege skab", "til at have ting i", "Kitchen Inc", SupplierCollection.getInstance().getSupplier(101), 57, Location.HardwareStore, 54, 600));
		this.items.add(new Item("Bøge bordplade", "til at have ting i", "Kitchen Inc", SupplierCollection.getInstance().getSupplier(102), 571, Location.HardwareStore, 24, 300));
		this.items.add(new Item("Ege bordplade", "til at have ting i", "Kitchen Inc", SupplierCollection.getInstance().getSupplier(102), 457, Location.HardwareStore, 50, 200));
	}

    public static ItemCollection getInstance()
    {
        return ItemCollection.instance;
    }

	public boolean addItem(Item newItem)
	{
		return this.items.add(newItem);
    }

	public ArrayList<Item> findItem(String name, String description, String manufacturer, Supplier supplier, Double price, Date acquisitionDate, Location location, int minimumStock, int maximumStock)
	{
		ArrayList<Item> foundItems = new ArrayList<Item>();

        for (Item item : this.items)
        {
			if(item.isActivated())
			{
				boolean found = false;
				if (name != null)
				{
					if(item.getName().toLowerCase().contains(name.toLowerCase()))
					{ found = true; }
				}
				else if (description != null)
				{
					if(item.getDescription().toLowerCase().contains(description.toLowerCase()))
					{ found = true; }
				}
				else if (manufacturer != null)
				{
					if(item.getManufacturer().toLowerCase().contains(manufacturer.toLowerCase()))
					{ found = true; }
				}
				else if (item.getSupplier().equals(supplier))
				{ found = true; }
				else if (item.getPrice() == price)
				{ found = true; }
				else if (acquisitionDate != null && item.getAcquisitionDate().compareTo(acquisitionDate) == 0)
				{ found = true; }
				else if (item.getLocation().equals(location))
				{ found = true; }
				else if (item.getMinimumStock() == minimumStock)
				{ found = true; }
				else if (item.getMaximumStock() == maximumStock)
				{ found = true; }

				if (found == true)
				{
					foundItems.add(item);
				}
			}
        }
		return foundItems;
	}

	public Item getItem(int itemNo)
	{
		for(Item item : this.items)
		{
			if(item.getItemNo() == itemNo)
			{
				return item;
			}
		}
		return null;
	}

    public ArrayList<Item> getAllItems()
    {
        ArrayList<Item> activatedItems = new ArrayList<Item>();
        for (Item item : this.items)
        {
            if(item.isActivated())
            {
                activatedItems.add(item);
            }
        }
        return activatedItems;
    }

	public void removeItem(Item item)
	{
        item.setActivated(false);
	}
}
